/**
 * 功能描述: TestHouseServiceImpl
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 */
package com.ce.pms.house.service;

import java.io.PrintStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import com.ce.pms.DBUnitTool;
import com.ce.pms.common.model.SysInfo;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.common.util.UpLoadFile;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.model.HouseDomain;
import com.ce.pms.house.model.HousePlan;
import com.ce.pms.house.vo.FlatVO;
import com.ce.pms.house.vo.HouseSearchVO;
import com.ce.pms.house.vo.HouseVO;
import com.ce.pms.house.vo.ImageVO;
import com.ce.pms.house.vo.PrecinctVO;
import com.sitechasia.webx.core.support.Page;

/**
 * 类名称：com.ce.pms.house.service.TestHouseServiceImpl  <br>
 * 功能描述：  <br>
 *
 * @author  付湘辉  <br>
 * Created on：  2008-7-8  <br>
 * @author  <br>
 * update on：  <br>
 * 修改原因：   <br>
 */
public class HouseServiceImplT extends DBUnitTool {
	private static IHouseService houseService;

	/**
	 * 功能描述:  <br>
	 *
	 * {@link com.ce.pms.house.service.impl.HouseServiceImpl#addHouse(FlatVO)}
	 * {@link com.ce.pms.house.service.impl.HouseServiceImpl#deleteHouse(Long, List)}
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-25  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@Ignore
	public void testAddHouse(){
        try {
            initHouseService();
            FlatVO flat = new FlatVO();
            flat.setBuildingId(1l);
            flat.setBuildSize(BigDecimal.valueOf(256.23));
            flat.setCreator(1l);
            flat.setDeliveryLimitationId(40l);
            flat.setDirectionId("75,77");
            flat.setFloor(1);
            flat.setHouseGrpid(1l);
            flat.setHouseNumber("12-1A-eraaa34rrsf234534");
            flat.setHousePlanId(1l);
            flat.setInnerSize(BigDecimal.valueOf(227.45));
            flat.setIsVirtual(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO);
            flat.setProjectId(1l);
            flat.setPublicSize(BigDecimal.valueOf(25.67));
            flat.setRemark(null);
            flat.setStatusId(34l);
            ImageVO image = new ImageVO();
            image.setName(generationRandomString()+".jpg");
//            flat.setIchnographyName(generationRandomString()+".jpg");
            image.setPath("uploadimages\\");
//            flat.setIchnographyPath("uploadimages\\");
            flat.setIchnographyVo(image);
            flat = houseService.addHouse(flat);
            Assert.assertNotNull(flat);
            Long newHouseId = flat.getKeyword();
            Assert.assertNotNull(newHouseId);

            List<ImageVO> imageList = new ArrayList<ImageVO>();
            houseService.deleteHouse(newHouseId, imageList);
            UpLoadFile.deleteFile(imageList);
            HouseVO house = houseService.getFlatHouseById(newHouseId);
            Assert.assertNull(house);
        } catch (Exception e) {

        }
	}

	@Ignore
    public void testCheckHouseStatusCanbeModified() {
        initHouseService();
        try {
            //传入空值
            HouseServiceImplT.houseService.checkHouseStatusCanbeModified(null);
        } catch (Exception e) {
            dealException(e,"common:parameters_null_exception, ",BusinessException.WARNING);
        }
        Boolean res = null;
        try {
            //不能修改的情况
            res = HouseServiceImplT.houseService.checkHouseStatusCanbeModified(1l);
            Assert.assertFalse(res);

            //能修改的情况
            res = HouseServiceImplT.houseService.checkHouseStatusCanbeModified(45l);
            Assert.assertTrue(res);
        } catch (Exception e) {
            dealException(e, "", "");
        }

    }

    /**
     *
     * 功能描述:   <br>
     *
     *
     * Create author：付湘辉  <br>
     * Create on:2008-8-3  <br>
     * Edit author：  <br>
     * Edit on:    <br>
     * Why:     <br>
     */
	@Ignore
    public void testCheckHouseWhetherWithinSameBuilding(){
        try {
            initHouseService();
            //在同一楼栋下
            Boolean res = HouseServiceImplT.houseService.checkHouseWhetherWithinSameBuilding("1,2,3");
            Assert.assertTrue(res);
            //不在同一楼栋下的情况
            res = HouseServiceImplT.houseService.checkHouseWhetherWithinSameBuilding("1,2,45");
            Assert.assertFalse(res);
        } catch (Exception e) {
            dealException(e);
        }
        //传入非法格式的字符串
        try {
            HouseServiceImplT.houseService.checkHouseWhetherWithinSameBuilding("1,2,3,");
        } catch (Exception e) {
            dealException(e,"common:id_join_string_format_exception",BusinessException.WARNING);
        }

    }

    /**
	 * 功能描述:  <br>
	 *
	 * {@link com.ce.pms.house.service.impl.HouseServiceImpl#checkIsOnlyHouseNoInHouseAndCoalitionHouse(String)}
	 * {@link com.ce.pms.house.service.impl.HouseServiceImpl#checkIsOnlyHouseNoInHouseAndCoalitionHouse(Long, String)}
	 * {@link com.ce.pms.house.service.impl.HouseServiceImpl#checkIsOnlyHouseNoInHouseAndCoalitionHouse(Long, Long, String)}
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-25  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@Ignore
	public void testCheckIsOnlyHouseNoInHouseAndCoalitionHouse(){
		initHouseService();
		boolean result = HouseServiceImplT.houseService.checkIsOnlyHouseNoInHouseAndCoalitionHouse("1-2A");
		Assert.assertTrue(result);
		result = HouseServiceImplT.houseService.checkIsOnlyHouseNoInHouseAndCoalitionHouse(1l,"1-2A");
		Assert.assertTrue(result);
		result = HouseServiceImplT.houseService.checkIsOnlyHouseNoInHouseAndCoalitionHouse(1l,2l,"1-2A");
		Assert.assertTrue(result);
	}

	@Ignore
    public void testDeleteHouse() {
        try {
            initHouseService();
        } catch (Exception e) {

        }
    }

    /**
     * 功能描述:  <br>
     *
     *
     * Create author:付湘辉 <br>
     * Create on:2008-8-5  <br>
     * Edit author:  <br>
     * Edit on:    <br>
     * Why:     <br>
     */
	@Ignore
    public void testEditHouse() {
        try {
            initHouseService();
            initHouseService();
            FlatVO flat = new FlatVO();
            flat.setBuildingId(1l);
            flat.setBuildSize(BigDecimal.valueOf(256.23));
            flat.setCreator(1l);
            flat.setDeliveryLimitationId(40l);
            flat.setDirectionId("75,77");
            flat.setFloor(1);
            flat.setHouseGrpid(1l);
            flat.setHouseNumber("12-1A-eraaa34rrsf234534");
            flat.setHousePlanId(1l);
            flat.setInnerSize(BigDecimal.valueOf(227.45));
            flat.setIsVirtual(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO);
            flat.setProjectId(1l);
            flat.setPublicSize(BigDecimal.valueOf(25.67));
            flat.setRemark(null);
            flat.setStatusId(34l);
            ImageVO image = new ImageVO();
            image.setName(generationRandomString()+".jpg");
//            flat.setIchnographyName(generationRandomString()+".jpg");
//            flat.setIchnographyPath("uploadimages\\");
            image.setPath("uploadimages\\");
            flat.setIchnographyVo(image);
            flat = houseService.addHouse(flat);
            Assert.assertNotNull(flat);
            Long newHouseId = flat.getKeyword();
            Assert.assertNotNull(newHouseId);
            flat = (FlatVO)houseService.getFlatHouseById(newHouseId);
            //进行修改
            image = flat.getIchnographyVo();
            image.setName(generationRandomString()+".gif");
//            flat.setIchnographyName(generationRandomString()+".gif");
            FlatVO resFlat = houseService.editHouse(flat);
            Assert.assertNotNull(resFlat);
            Assert.assertEquals(newHouseId, resFlat.getKeyword());

            List<ImageVO> imageList = new ArrayList<ImageVO>();
            houseService.deleteHouse(newHouseId, imageList);
            UpLoadFile.deleteFile(imageList);
        } catch (Exception e) {
        	dealException(e);
        }
    }

    /**
	 * 功能描述:  <br>
	 *
	 * {@link com.ce.pms.house.service.impl.HouseServiceImpl#getFlatHouseById(Long)}
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-25  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@Ignore
	public void testGetFlatHouseById(){
		Long houseId = Long.valueOf(1l);
		initHouseService();
		HouseVO houseVo = houseService.getFlatHouseById(houseId);

		Assert.assertNotNull(houseVo);
		Assert.assertEquals(houseId, houseVo.getKeyword());

	}

	@Ignore
    public void testGetHouseAndCustomerBy() {
        try {

        } catch (Exception e) {

        }
    }

	@Ignore
    public void testGetHouseByNumber() {
        try {

        } catch (Exception e) {

        }
    }

    /**
	 * 功能描述:  <br>
	 *
	 * {@link com.ce.pms.house.service.impl.HouseServiceImpl#getHouseDomainList(String)}
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-25  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@Ignore
	public void testGetHouseDomainList(){
		initHouseService();
		@SuppressWarnings("unused")
		List<HouseDomain> list = HouseServiceImplT.houseService.getHouseDomainList("1-2A");
		Assert.assertNotNull(list);
	}
	@Ignore
    public void testGetHouseModels() {
        try {
            initHouseService();
            List<HousePlan> list = HouseServiceImplT.houseService.getHouseModels();
            Assert.assertNotNull(list);
        } catch (Exception e) {

        }
    }

	@Ignore
    public void testGetHouseStatuses() {
        try {
            initHouseService();
            List<SysrefcodeDO> list = HouseServiceImplT.houseService.getHouseStatuses();
            Assert.assertNotNull(list);
            Assert.assertTrue(list.size()>0);
        } catch (Exception e) {

        }
    }

	@Ignore
    public void testGetOwnerNameBy() {
        try {
            initHouseService();
            //有业主的房间
            String ownerName = HouseServiceImplT.houseService.getOwnerNameBy("1-2A");
            Assert.assertNotNull(ownerName);

            //没有业主的房间
            ownerName = HouseServiceImplT.houseService.getOwnerNameBy("7-2C");
            Assert.assertNotNull(ownerName);

        } catch (Exception e) {

        }
    }
	
	/**
	 * 功能描述:  <br>
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2009-1-19  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@Test
	public void testGetPrecinctVOByHouseId() {
		try {
			initHouseService();
			//正常业务
			//用一个管理房查
			PrecinctVO precinctVO = houseService.getPrecinctVOByHouseId(manageHouseId, null);
			Assert.assertNotNull(precinctVO);
			precinctVO = houseService.getPrecinctVOByHouseId(manageHouseId, "1");
			Assert.assertNotNull(precinctVO);
			//用产权房查
			precinctVO = houseService.getPrecinctVOByHouseId(proRightHouseId, null);
			Assert.assertNotNull(precinctVO);
			houseService.getPrecinctVOByHouseId(proRightHouseId, "0");
			Assert.assertNotNull(precinctVO);
			//传入不合法参数
		} catch (Exception e) {
			super.dealException(e);
		}
	}
	/**
	 * 管理房id(有有效账户)
	 */
	private static Long manageHouseId=61l;
	
	/**
	 * 产权房id(有有效账户)
	 */
	private static Long proRightHouseId=1l;
	

	/**
	 * 功能描述:  <br>
	 *
	 * {@link com.ce.pms.house.service.impl.HouseServiceImpl#modifyHouseStatus(Long, Long)}
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-25  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@Ignore
	public void testModifyHouseStatus(){
		initHouseService();
		StringBuilder valueString = new StringBuilder();
		valueString.append("'").append(SysrefcodeDO.HM_HOUSE_HOUSE_STATUS_VALUE_RENTAL).append("'");
		valueString.append(SysInfo.SEPARATOR_COMMA);
		valueString.append("'").append(SysrefcodeDO.HM_HOUSE_HOUSE_STATUS_VALUE_SINCETHELIVING).append("'");
		valueString.append(SysInfo.SEPARATOR_COMMA);
		valueString.append("'").append(SysrefcodeDO.HM_HOUSE_HOUSE_STATUS_VALUE_VACANT).append("'");
		List<SysrefcodeDO> list = HouseServiceImplT.houseService.getHouseStatuses(valueString.toString());

		Assert.assertNotNull(list);
		Assert.assertNotNull(list.size() > 0);

		FlatVO house = (FlatVO)HouseServiceImplT.houseService.getFlatHouseById(18l);
		Long statusId = house.getStatusId();

		try{
			house = (FlatVO)HouseServiceImplT.houseService.modifyHouseStatus(house.getKeyword(), 38l);
			Assert.assertNotNull(house);
			HouseServiceImplT.houseService.modifyHouseStatus(house.getKeyword(), statusId);
		}catch(Exception e){
			e.printStackTrace();
		}

	}

	/**
	 * 功能描述:  <br>
	 *
	 * {@link com.ce.pms.house.service.impl.HouseServiceImpl#queryDifferentStatusHouseStatusListBybuildingId(Long)}
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-25  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@Ignore
	public void testQueryDifferentStatusHouseStatusListBybuildingId(){

		initHouseService();
		List list = HouseServiceImplT.houseService.queryDifferentStatusHouseStatusListBybuildingId(1l);

		Assert.assertNotNull(list);
		System.out.println(list.size());
		Assert.assertTrue(list.size()==7);

	}

	/**
	 * 功能描述:  <br>
	 *
	 * {@link com.ce.pms.house.service.impl.HouseServiceImpl#queryHouseIconListByPage(HouseSearchVO, Integer, Integer)}
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-25  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@Ignore
	public void testQueryHouseIconListByPage(){
		initHouseService();
		HouseSearchVO searchVO = new HouseSearchVO();
		searchVO.setBuildingId(1l);

		Map map = HouseServiceImplT.houseService.queryHouseIconListByPage(searchVO, 1, 9);
		Assert.assertNotNull(map);
		Assert.assertEquals(3, map.size());
		List list = (List)map.get(1);
	}

	@Ignore
    public void testQueryHouseListBy() {
        try {
            initHouseService();
            HouseSearchVO searchVO = new HouseSearchVO();
            searchVO.setBuildingId(1l);
            searchVO.setHouseGrpid(1l);
            List<FlatVO> list = HouseServiceImplT.houseService.queryHouseListBy(searchVO);
            Assert.assertNotNull(list);
        } catch (Exception e) {

        }
    }

	/**
	 * 功能描述:  <br>
	 *
	 * {@link com.ce.pms.house.service.impl.HouseServiceImpl#queryHouseListByPage(com.ce.pms.house.vo.HouseSearchVO, Integer, Integer)} <br>
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-25  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@Ignore
	public void testQueryHouseListByPage(){
		initHouseService();
		HouseSearchVO searchVO = new HouseSearchVO();
		searchVO.setBuildingId(1l);

		Page page = HouseServiceImplT.houseService.queryHouseListByPage(searchVO, 1, 15);
		Assert.assertNotNull(page);

		List list = page.getResult();
		long totalcount= page.getTotalCount();

	}

	@Ignore
	public void testQueryNextHouseById() {
		try {
			initHouseService();
			Long houseId = 1l;
			HouseVO housevo = houseService.queryNextHouseById(houseId);

			System.out.println(housevo==null?"null":housevo.getKeyword());
		} catch (Exception e) {
			dealException(e);
		}
	}

    /**
     *
     * 功能描述:   <br>
     *
     *
     * Create author：付湘辉  <br>
     * Create on:2008-8-3  <br>
     * Edit author：  <br>
     * Edit on:    <br>
     * Why:     <br>
     */
	private void initHouseService(){
		if (HouseServiceImplT.houseService == null) {
			HouseServiceImplT.houseService = (IHouseService)super.applicationContext.getBean("houseService");
		}
	}
}
